/**
 * Created by 15c on 2017/12/2.
 */
//获取页面跳转传过来的参数值
//function request(params) {
//
//    var url = location.href;
//
//    var paraString = url.substring(url.indexOf("?") + 1, url.length).split("&");
//
//    var paraObj = {};
//
//    for (i = 0; j = paraString[i]; i++) {
//
//        paraObj[j.substring(0, j.indexOf("=")).toLowerCase()] = j.substring(j.indexOf("=") + 1, j.length);
//
//    }
//
//    var returnValue = paraObj[params.toLowerCase()];
//
//    if (typeof (returnValue) == "undefined") {
//
//        return "";
//
//    } else {
//
//        return returnValue;
//
//    }
//
//}
//登录用户的userId
var userId;
var url = 'http://www.xiaowenxiong.cn/test';
//服务器图片路径
var imgUrl = url + "/resources/img/";
$(document).ready(function () {
    userId = window.sessionStorage.getItem(loginUserId);
    if (userId) {
        getUserInfo(userId);
    }
    getNewStrategy();
    getNewTravel();
    getManyLikeStrategy();
    getManyLikeTravel();

});
//获取登录成功用户的头像以及email
function getUserInfo(userId) {
    $.ajax({
        type: 'GET',
        url: url + '/users/details/show',
        data: {
            userId: userId
        },
        dataType: 'json',
        timeout: 50000,
        success: function (result) {
            console.log(result);
            if (result.error == 0) {
                $("#user").css("display", "block");
                var data = result.data;
                if (data.hasOwnProperty("userAvatarAddress") && data.userAvatarAddress != '') {
                    $("#user_avt").attr('src', imgUrl + data.userAvatarAddress);
                }
                $("#user_email").text(data.userEmail);
                $(".user_page_view").attr('href', '../personCenter/person-view.html');
            } else {
                alert('数据请求有点小错误，刷新试试');
            }

        },
        error: function (xhr, type) {

            alert('请求出错，再刷新试试');
        }
    });
}
//获得最新五条攻略类型游记
function getNewStrategy() {
    $.ajax({
        type: 'GET',
        url: url + '/travels/getNewStrategy',
        dataType: 'json',
        success: function (result) {
            console.log(result);
            if (result.error == 0) {
                var data = result.data;
                var imgAvt;
                for (var i = 0; i < data.length; i++) {
                    if ((data[i].travelCreatorUser).hasOwnProperty("userAvatarAddress") && (data[i].travelCreatorUser.userAvatarAddress) != ''){
                        imgAvt=imgUrl+data[i].travelCreatorUser.userAvatarAddress;

                    }else{
                        imgAvt='../../resources/travel-note/img/avt.jpg';
                    }
                    $("#strategy_recommendation").append(
                        "<li  class='col-md-12 col-xs-12'  >" +
                        "<a class='media-left col-md-2 col-xs-4' href='../personCenter/other-view.html?userId=" + data[i].travelCreatorUser.userId + "'><img class='img-circle col-md-12 col-xs-12' style='height: 60px;width: 90px' src='"+imgAvt+"'></a>" +
                        "<div class='col-md-10 col-xs-8'>" +
                        "<a href='../travel-note/note-content.html?travleId="+data[i].travleId+"'><span class='col-md-9 col-xs-12'  style=' font-size: 15px;line-height: 70px'>" + data[i].travelTitle + "</span></a>" +
                        "<div class='col-md-3 col-xs-12' >" +
                        "<br/>"+
                        "<span class='glyphicon glyphicon-thumbs-up col-md-12 col-xs-6' style='color: rgb(73, 75, 73);font-size: 15px;'>" + data[i].travelLikeNum + "</span>" +
                        "<span class='glyphicon glyphicon-eye-open col-md-12 col-xs-6' style='color: rgb(73, 75, 73); font-size: 15px;'>" + data[i].travelPageView + "</span>" +
                        "</div>" +
                        "</div>" +
                        "</li>");
                }
            } else if (result.error == 112) {
                console.log("攻略类型游记查询为空！");
            } else {
                alert('数据请求出了点问题，刷新试试');
            }

        },
        error: function (xhr, type) {

            alert('请求出错，再刷新试试');
        }
    });
}
//获得最新五条其他类型游记
function getNewTravel() {
    $.ajax({
        type: 'GET',
        url: url + '/travels/getNewTravel',
        dataType: 'json',
        success: function (result) {
            console.log(result);
            if (result.error == 0) {
                var data = result.data;
                var imgAvt;
                for (var i = 0; i < data.length; i++) {
                    if ((data[i].travelCreatorUser).hasOwnProperty("userAvatarAddress") && (data[i].travelCreatorUser.userAvatarAddress) != ''){
                        imgAvt=imgUrl+data[i].travelCreatorUser.userAvatarAddress;

                    }else{
                        imgAvt='../../resources/travel-note/img/avt.jpg';
                    }
                    $("#travel_recommendation").append(
                    "<li  class='col-md-12 col-xs-12'  >" +
                        "<a class='media-left col-md-2 col-xs-4' href='../personCenter/other-view.html?userId=" + data[i].travelCreatorUser.userId + "'><img class='img-circle col-md-12 col-xs-12' style='height: 60px;width: 90px' src='"+imgAvt+"'></a>" +
                        "<div class='col-md-10 col-xs-8'>" +
                            "<a href='../travel-note/note-content.html?travleId="+data[i].travleId+"'><span class='col-md-9 col-xs-12'  style=' font-size: 15px;line-height: 70px'>" + data[i].travelTitle + "</span></a>" +
                            "<div class='col-md-3 col-xs-12' >" +
                                "<br/>"+
                                "<span class='glyphicon glyphicon-thumbs-up col-md-12 col-xs-6' style='color: rgb(73, 75, 73);font-size: 15px;'>" + data[i].travelLikeNum + "</span>" +
                                "<span class='glyphicon glyphicon-eye-open col-md-12 col-xs-6' style='color: rgb(73, 75, 73); font-size: 15px;'>" + data[i].travelPageView + "</span>" +
                            "</div>" +
                        "</div>" +
                    "</li>");
                }
            } else if (result.error == 113) {
                console.log("其他类型游记查询为空！");
            } else {
                alert('数据请求出了点问题，刷新试试');
            }

        },
        error: function (xhr, type) {

            alert('请求出错，再刷新试试');
        }
    });
}
//获得最新4条点赞最多的攻略类型游记
function getManyLikeStrategy() {
    $.ajax({
        type: 'GET',
        url: url + '/travels/getManyLikeStrategy',
        dataType: 'json',
        success: function (result) {
            console.log(result);
            if (result.error == 0) {
                var title = $(".strategy_title_like");
                var like = $(".strategy_like_like");
                var view = $(".strategy_view_like");
                var time = $(".strategy_time_like");
                var email = $(".strategy_email_like");
                var content = $(".strategy_content_like");

                var data = result.data;
                for (var i = 0; i < data.length; i++) {
                    $(title[i]).text(data[i].travelTitle);
                    $(like[i]).text(data[i].travelLikeNum);
                    $(view[i]).text(data[i].travelPageView);
                    $(time[i]).text(dateFormat_1(data[i].travelIssueTime));
                    $(email[i]).text(data[i].travelCreatorUser.userEmail);
                    $(content[i]).attr('href', '../travel-note/note-content.html?travleId=' + data[i].travleId);
                }

            } else if (result.error == 114) {
                console.log("点赞最多的攻略类型游记查询为空！");
            } else {
                alert('数据请求出了点问题，刷新试试');
            }

        },
        error: function (xhr, type) {

            alert('请求出错，再刷新试试');
        }
    });
}
//获得最新4条点赞最多的其他类型游记
function getManyLikeTravel() {
    $.ajax({
        type: 'GET',
        url: url + '/travels/getManyLikeTravel',
        dataType: 'json',
        success: function (result) {
            console.log(result);
            if (result.error == 0) {
                var title = $(".travel_title_like");
                var like = $(".travel_like_like");
                var view = $(".travel_view_like");
                var time = $(".travel_time_like");
                var email = $(".travel_email_like");
                var content = $(".travel_content_like");

                var data = result.data;
                for (var i = 0; i < data.length; i++) {
                    $(title[i]).text(data[i].travelTitle);
                    $(like[i]).text(data[i].travelLikeNum);
                    $(view[i]).text(data[i].travelPageView);
                    $(time[i]).text(dateFormat_1(data[i].travelIssueTime));
                    $(email[i]).text(data[i].travelCreatorUser.userEmail);
                    $(content[i]).attr('href', '../travel-note/note-content.html?travleId=' + data[i].travleId);
                }


            } else if (result.error == 115) {
                console.log("点赞最多的其他类型游记查询为空！");

            } else {
                alert('数据请求出了点问题，刷新试试');
            }

        },
        error: function (xhr, type) {

            alert('请求出错，再刷新试试');
        }
    });
}

//返回yyyy-MM-dd格式日期（一）：
function dateFormat_1(longTypeDate) {
    var dateType = "";
    var date = new Date();
    date.setTime(longTypeDate);
    dateType = date.getFullYear() + "-" + getMonth(date) + "-" + getDay(date);//yyyy-MM-dd格式日期
    return dateType;
}

//返回 01-12 的月份值
function getMonth(date) {
    var month = "";
    month = date.getMonth() + 1; //getMonth()得到的月份是0-11
    if (month < 10) {
        month = "0" + month;
    }
    return month;
}

//返回01-30的日期
function getDay(date) {
    var day = "";
    day = date.getDate();
    if (day < 10) {
        day = "0" + day;
    }
    return day;
}